import numpy as np
import matplotlib.pyplot as plt
import cv2
from conv_net import ConvNet

num=4
img=plt.imread("./testpicture/"+str(num)+".jpg",1)
img_focus=img[485:1682,0:1197]
img_focus=cv2.resize(img_focus,[270,270],cv2.INTER_LINEAR)
img_focus_gray=cv2.cvtColor(img_focus,cv2.COLOR_BGR2GRAY)
img_focus_canny=cv2.Canny(img_focus,100,200)
col_count=np.sum(img_focus_canny,axis=1)
y_start=0
y_end=269
network=ConvNet()
network.load_params("2.pkl")

for i in range(270//2):
    if(col_count[i]>10):
        y_start=i
        break
for i in range(270//2):
    if (col_count[269-i] > 200):
        y_end=269-i
        break

w = img_focus.shape[0]
h = y_end-y_start+3
split_w=w//9
split_h_num=h//(split_w)
split_h=split_w

def divide_ij(img_focus):#没办法还是用来自己写的for循环
    image_array = np.zeros([split_h_num,9,split_w,split_h,3]).astype("uint8")
    for i in range(split_h_num):
        for j in range(9):
            image_array[i][j]=img_focus[y_start+i*split_h:y_start+(i+1)*split_h,j*split_w:(j+1)*split_w]
    return image_array

image_array=divide_ij(img_focus)


image_array1=image_array.reshape(image_array.shape[0]*image_array.shape[1],image_array.shape[2],image_array.shape[3],image_array.shape[4])
image_array1=image_array1.transpose(0,3,1,2)/255

result=network.predict(image_array1,train_flg=False)
index1=np.argmax(result,axis=1)+1
index1=index1.reshape(split_h_num,9)


plt.imshow(img_focus)

def indexij(i,j):
    if (i<0) or (i>split_h_num-1) or (j<0) or (j>8):
        return 0
    else:
        return index1[i,j]

def indexij2(i, j):
            if (i < 0) or (i > split_h_num - 1) or (j < 0) or (j > 8):
                return 0
            else:
                return index2[i, j]

swapij=[[-1,-1],[-1,-1]]
stopflag=0
for i in range(split_h_num).__reversed__():
    if stopflag == 1:
        stopflag=0
        break
    for j in range(9):
        if index1[i][j]==10:
            continue
        else:
            a=indexij(i,j)
            if indexij(i,j+1)==a:
                if indexij(i-1, j + 2)==a:
                    swapij=[[i-1,j+2],[i,j+2]]
                elif indexij(i, j + 3)==a:
                    swapij=[[i,j+3],[i,j+2]]
                elif indexij(i+1,j+2)==a:
                    swapij=[[i+1,j+2],[i,j+2]]
            else:
                if (indexij(i-1, j + 1)==a) and (indexij(i, j + 2)==a):
                    swapij = [[i - 1, j + 1], [i, j + 1]]
                elif (indexij(i-1, j + 1)==a) and (indexij(i+1, j + 1)==a):
                    swapij = [[i - 1, j + 1], [i+1, j + 1]]
                elif (indexij(i, j + 2)==a) and (indexij(i+1, j + 1)==a):
                    swapij = [[i , j + 1], [i + 1, j + 1]]
                elif (indexij(i-1, j + 1)==a) and (indexij(i-2, j + 1)==a):
                    swapij = [[i, j], [i, j + 1]]
                elif (indexij(i, j + 2)==a) and (indexij(i, j + 3)==a):
                    swapij = [[i, j], [i, j + 1]]
                elif (indexij(i+1, j + 1)==a) and (indexij(i+2, j + 1)==a):
                    swapij = [[i, j], [i, j + 1]]
            if swapij[0][0]==-1:
                continue
            else:
                stopflag = 1
                break

    if stopflag == 0:

        print(1111111111)
        index2 = cv2.flip(index1, 1)  # 水平翻转
        for i in range(split_h_num).__reversed__():
            if stopflag == 1:
                break
            for j in range(9):
                if index2[i][j] == 10:
                    continue
                else:
                    a = indexij2(i, j)
                    if indexij2(i, j + 1) == a:
                        if indexij2(i - 1, j + 2) == a:
                            swapij = [[i - 1, j + 2], [i, j + 2]]
                        elif indexij2(i, j + 3) == a:
                            swapij = [[i, j + 3], [i, j + 2]]
                        elif indexij2(i + 1, j + 2) == a:
                            swapij = [[i + 1, j + 2], [i, j + 2]]
                    elif indexij2(i, j + 1) != 10:
                        if (indexij2(i - 1, j + 1) == a) and (indexij2(i, j + 2) == a):
                            swapij = [[i - 1, j + 1], [i, j + 1]]
                        elif (indexij2(i - 1, j + 1) == a) and (indexij2(i + 1, j + 1) == a):
                            swapij = [[i, j], [i, j + 1]]
                        elif (indexij2(i, j + 2) == a) and (indexij2(i + 1, j + 1) == a):
                            swapij = [[i, j + 1], [i + 1, j + 1]]
                        elif (indexij2(i - 1, j + 1) == a) and (indexij2(i - 2, j + 1) == a):
                            swapij = [[i, j], [i, j + 1]]
                        elif (indexij2(i, j + 2) == a) and (indexij2(i, j + 3) == a):
                            swapij = [[i, j], [i, j + 1]]
                        elif (indexij2(i + 1, j + 1) == a) and (indexij2(i + 2, j + 1) == a):
                            swapij = [[i, j], [i, j + 1]]
                    if swapij[0][0] == -1:
                        continue
                    else:
                        stopflag = 1
                        swapij[0][0] = swapij[0][0]
                        swapij[0][1] = 8 - swapij[0][1]
                        swapij[1][0] = swapij[1][0]
                        swapij[1][1] = 8 - swapij[1][1]
                        break



print(swapij)












# img_test=image_array[1][4]
# plt.figure()
# plt.imshow(img_test)
# result=network.predict(img_test.transpose(2,0,1).reshape(1,3,30,30),train_flg=False)
# index2=np.argmax(result,axis=1)+1


# plt.imshow(img_focus_canny,cmap="gray")
# plt.show()